<?php
class WiktionnaryParser extends Parser{
	
	public function __construct($url){
		parent::__construct($url);
	}
	
	public function extract_content($html){
		$this->extract_outline($html);
		$next = $this->extract_introduction($html);
		$this->extract_sections($next);
	}
	
	protected function keep_link($html){
		if (parent::keep_link($html) == false) return false;
		if(strpos($html->getAttribute('href'), 'Wiktionnaire:') !== false){
			return false;
		}
		return true;
	}
	
	protected function extract_node($node, $parent = null){
		switch($node->nodeName){
			case 'dl' : {
				return $this->extract_dl($node, $parent);
			}
			case 'button' : {
				if(strpos($node->parentNode->parentNode->getAttribute('id'), 'ogg_player') !== false){
					return $this->extract_ogg($node, $parent);
					break;
				}
			}
			default : {
				parent::extract_node($node, $parent);
			}
		}
	}
	
	protected function extract_header($node, $parent = null){
		if($node != null){
			$header = new Header((int)substr($node->nodeName, 1, 1));
			foreach($node->childNodes as $child){
				$this->extract_header_element($child, $header);
			}
			if($parent != null){
				$parent->add_element($header);
			}
			return $header;
		}
		return null;
	}
	
	protected function extract_header_element($node, $parent){
		if($node != null){
			if($node->nodeName == '#text'){
				$this->extract_element_node($node, $parent);
			}else if($this->isAddable($node)){
				foreach($node->childNodes as $child){
					$this->extract_header_element($child, $parent);
				}
			}
		}
		return null;
	}
	
	protected function extract_ancre($node){
		$deb = strpos($node->getAttribute('href'),'#');
		$href = substr($node->getAttribute('href'),$deb,strlen($node->getAttribute('href')) - $deb);
		 $link = new Link($href, $node->getAttribute('name'));
		 $link->add_element(new Text($node->getElementsByTagName('span')->item(1)->textContent));
		 return $link;
	}
	
	protected function extract_list($node, $parent = null){
		$liste = parent::extract_list($node, $parent);
		$links = $liste->separate_links($this->url);
		$e = new Element();
		$e->add_classname('liens');
		if(get_class($parent) == 'ElementList'){
			$links = array_merge($links, $parent->separate_links($this->url));
		}
		$this->add_link_list($links, $e);
		if($parent != null && sizeof($e->get_elements()) > 0){
			$parent->add_element($e);
		}
		return $liste;
	}
	
	private function extract_dl($node, $parent){
		if(get_class($parent) != 'ElementList'){
			foreach($node->childNodes as $dd){
				if($node->nodeType == XML_ELEMENT_NODE) $this->extract_paragraph($dd, $parent);
			}
		}else{
			$liste = new Liste(Liste::UL);
			foreach($node->childNodes as $dd){
				if($dd->nodeName =='dd')	$this->extract_element_list($dd, $liste);
			}
			$parent->add_element($liste);
			return $liste;
		}
	}
	
	private function extract_ogg($node, $parent = null){
		$script = $node->getAttribute('onclick');
		$deb = strpos($script,'http://upload.wikimedia.org/wikipedia/commons/');
		$lg = strpos($script,'.ogg') - $deb + 4;
		$url = substr($script,$deb,$lg);
		$audio = new Audio($url);
		if($parent != null){
			$parent->add_element($audio);
		}
		return $audio;
	}
	
	protected function extract_table($node, $parent = null){
		if($this->keep_table($node)){
			return parent::extract_table($node, $parent);
		}
	}
	
	protected function keep_table($node){
		if($node->nodeName == 'a' && !$this->keep_link($node)){
			return false;
		}
		if($node->nodeName == '#text') return true;
		foreach($node->childNodes as $child){
			if(!$this->keep_table($child)){
				return false;
			}
		}
		return true;
	}
	
	
}
?>